package com.vgooo.client.myvg.weibo;

import java.text.SimpleDateFormat;
import java.util.Set;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

import com.vgooo.client.Main;
import com.vgooo.client.myvg.OtherLogin;
import com.vgooo.client.myvg.OtherLogin.LoginListener;
import com.vgooo.client.myvg.weibo.keeper.AccessTokenKeeper;
import com.vgooo.util.UmengUtils;
import com.weibo.sdk.android.Oauth2AccessToken;
import com.weibo.sdk.android.Weibo;
import com.weibo.sdk.android.WeiboAuthListener;
import com.weibo.sdk.android.WeiboDialogError;
import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.util.Utility;

public class WeiBoOauthorizeAction  {
	private static final String className = "WeiBoOauthActivity" ;
	
	private Weibo mWeibo ;
	private static final String CONSUMER_KEY = "2271382318" ;
	private static final String REDIRECTP_URL = "http://www.vgooo.com/user/login/sina" ;
	private static Oauth2AccessToken accessToken ;
	
//	private SsoHandler mSsoHandler ;
	
	public static boolean  isLogin = false ;
	private Activity activity ;
	private String tag ;
	
	private LoginListener listener ;
	
	public WeiBoOauthorizeAction(Activity activity, String tag,LoginListener listener ) {
		this.activity = activity;
		this.tag = tag;
		this.listener = listener ;
		init() ;
	}


//	public WeiBoOauthorizeAction(VGActivity cparent, String className2,
//			LoginListener loginListener) {
//		// TODO Auto-generated constructor stub
//	}


	private  void init() {
		// TODO Auto-generated method stub
		mWeibo = Weibo.getInstance(CONSUMER_KEY, REDIRECTP_URL);
		mWeibo.accessToken = new Oauth2AccessToken();
		mWeibo.authorize(activity, new AuthDialogListener());
		
		accessToken = AccessTokenKeeper.readAccessToken(Main.activity);
		if(accessToken.isSessionValid()){
			Weibo.isWifi = Utility.isWifi(activity);
			try {
				Class sso = Class.forName("com.weibo.sdk.android.api.WeiboAPI");
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
			}
//			 String date = new java.text.SimpleDateFormat("yyyy/MM/dd hh:mm:ss")
//             .format(new java.util.Date(accessToken
//                     .getExpiresTime()));
			
		}else{
		}
	}
	
	
	public static void cancelToken(){
		AccessTokenKeeper.clear(Main.activity);
	}
	
	@SuppressWarnings("unused")
	private class AuthDialogListener implements WeiboAuthListener{


		@Override
		public void onComplete(Bundle values) {
			// TODO Auto-generated method stub
			Set<String> keys = values.keySet();
			for(String key:keys){
			}
			String token = values.getString("access_token");
			String expires_in = values.getString("expires_in");
			WeiBoOauthorizeAction.accessToken = new Oauth2AccessToken(token, expires_in);
			if(WeiBoOauthorizeAction.accessToken.isSessionValid()){
				Toast.makeText(activity, "微博认证成功！", Toast.LENGTH_SHORT).show();
				String date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new java.util.Date(WeiBoOauthorizeAction.accessToken.getExpiresTime()));
				//保存Token
//				AccessTokenKeeper.keepAccessToken(Main.activity, accessToken);
				
				isLogin = true ;
				
				OtherLogin.OtherLogin(activity, token, OtherLogin.TYPE_WEI_BO, listener);
			}
		}

		@Override
		public void onError(WeiboDialogError e) {
			// TODO Auto-generated method stub
		}
		@Override
		public void onCancel() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onWeiboException(WeiboException e) {
			// TODO Auto-generated method stub
			Toast.makeText(activity, "微博认证失败！", Toast.LENGTH_SHORT).show();
			UmengUtils.onError(activity, e.toString());
			
		}
		
	}
	
	
	
	
//	private static void log(String info){
//		Log.i(className, info);
//	}
}
